package TUILayer;

import ControlLayer.LeaseCtr;
import ModelLayer.RealException;

public class LeaseItemMenuUI {
	
	private LeaseCtr leaseCtr;
	private InputParser inputParser;
	private int leaseId;
	
	public LeaseItemMenuUI(InputParser inputParser) {
		leaseCtr = new LeaseCtr();
		this.inputParser = inputParser;
	}
	public void start(int leaseId) {
		this.leaseId = leaseId;
		boolean exit = false;
		int choise = 0;
		do {
				try {
					choise = inputParser
					.getInt("\f ***LeaseItemUnitMenu*** \n\n1. Add lease item unit\n2. Delete lease item unit\n3. List all lease item units\n4. Return\nPlease make your choice: ");
				
					switch (choise) {
					case 1:
						addLeaseItemUnit();
						break;
					case 2:
						deleteLeaseItemUnit();
						break;
					case 3:
						showAllLeaseItemUnits();
						break;
					case 4:
						exit = true;
						break;
						}
					}
				catch (RealException e) {
						System.out.println("\n" + e.getError() + "Please try again");
						inputParser.printReturn();
				}
			}while (!exit);
		}
	
	public void addLeaseItemUnit() throws RealException
	{
		int leaseItemId = inputParser.getInt("\f*** AddLeaseItemUnitMenu***\n\nPlease input the lease item id: ");
		int serialNo = inputParser.getInt("\f*** AddLeaseItemUnitMenu***\n\nPlease input the serial number of the lease item unit: ");
		leaseCtr.addLeaseItemUnit(leaseId, leaseItemId, serialNo);
		showAllLeaseItemUnits();
	}
	
	public void deleteLeaseItemUnit() throws RealException
	{
		int leaseItemId = inputParser.getInt("\f*** AddLeaseItemUnitMenu***\n\nPlease input the lease item id: ");
		int serialNo = inputParser.getInt("\f*** AddLeaseItemUnitMenu***\n\nPlease input the serial number of the lease item unit: ");
		leaseCtr.removeLeaseItemUnit(leaseId, leaseItemId, serialNo);
		showAllLeaseItemUnits();
	}
	
	public void showAllLeaseItemUnits() throws RealException
	{
		System.out.println("\f *** LeaseMenu ***\n\n");
		if (leaseCtr.getlease(leaseId).listAllLeaseItemUnits() != null) {
			System.out.println(leaseCtr.getlease(leaseId).listAllLeaseItemUnits());
		}
		else {
			System.out.println("\nNo lease item unit has been added.");
		}
		inputParser.printReturn();
	}

}
